package com.company.project.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mongo")
public class MongoController {

	private final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private MongoTemplate mongoTemplate;

	@GetMapping("/test")
	public String test() {

		List<AggregationOperation> operations = new ArrayList<>();

		operations.add(Aggregation.group("activity").sum("0200_sw_s").as("0200pv"));
		operations.add(Aggregation.group("activity").sum("0202_sw_s").as("0200pv"));

		Aggregation aggregation = Aggregation.newAggregation(operations);

		//查询、并获取结果
		AggregationResults<Map> studentScore = mongoTemplate.aggregate(aggregation, "EventStatisticsDataByDay", Map.class);

		System.out.println(studentScore.getRawResults());

		return "";
	}


}
